package com.douwong.student.fragment;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseFragment;
import com.douwong.student.model.ClassTestRecordInfos;
import com.douwong.student.model.ExamResult;
import com.douwong.student.model.ListPlExamineItemBean;
import com.douwong.student.popuwindow.ClassTestRecordPopuWindow;
import com.douwong.student.utils.HtmlUtils;
import com.douwong.student.utils.UIUtils;

import java.util.List;

/**
 * @autor yichao
 * @time 2017/5/1017:16
 * @des 测验记录判读题
 */

public class TestClassRecordJudgeFragment extends BaseFragment {


    private View mContentView;
    private ImageView mIvHead;
    private AppCompatTextView mAppCompatTextView;
    private LinearLayout mLlTitleRoot;
    private ListPlExamineItemBean mListPlExamineItemBean;
    private List<ExamResult> mExamResultList;
    private TextView mTv_judgment_right_question;
    private TextView mTv_judgment_wrong_question;
    private ClassTestRecordPopuWindow mShowClassTestRecordPopuWindown;
    private Context mContext;
    private ClassTestRecordInfos.DataBean.ExaminResultBean mExaminResultBean;
    private final ClassTestRecordInfos.DataBean.ExaminResultBean.IteminfoBean mIteminfo;
    private final List<String> mAnswer;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public TestClassRecordJudgeFragment(ClassTestRecordInfos.DataBean.ExaminResultBean examinResultBean) {
        super();
        mExaminResultBean = examinResultBean;
        mIteminfo = examinResultBean.getIteminfo();
        mAnswer = examinResultBean.getAnswer();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  mShowClassTestRecordPopuWindown = new ClassTestRecordPopuWindow(mContext,mIteminfo);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowClassTestRecordPopuWindown = new ClassTestRecordPopuWindow(mContext,mIteminfo);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContentView = UIUtils.inflate(getActivity(), R.layout.test_record_judgment_questions);
        mIvHead = (ImageView) mContentView.findViewById(R.id.iv_answer_show_pic);
        mAppCompatTextView = (AppCompatTextView) mContentView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContentView.findViewById(R.id.ll_choicefragme_title_root);
        mTv_judgment_right_question = (TextView) mContentView.findViewById(R.id.tv_Judgment_right_question);
        mTv_judgment_wrong_question = (TextView) mContentView.findViewById(R.id.tv_Judgment_wrong_question);
        return mContentView;
    }

    @Override
    protected void initData() {
        super.initData();
        if (mIteminfo!=null){
            String itemBody = mIteminfo.getItem_body();
            //是否显示图片
            mIvHead.setVisibility(HtmlUtils.isShowImage(itemBody)?View.VISIBLE:View.GONE);
            //题目
            HtmlUtils.showItemBody(mAppCompatTextView,itemBody);

            if (mAnswer == null ||mAnswer.size()==0) {
                return;
            }
            for (int i = 0; i < mAnswer.size(); i++) {

                        if (mAnswer.get(i).equalsIgnoreCase("A")) {
                            mTv_judgment_right_question.setBackgroundResource(R.mipmap.answer_right_r);

                        } else if (mAnswer.get(i).equalsIgnoreCase("B")) {
                            mTv_judgment_wrong_question.setBackgroundResource(R.mipmap.answer_wrong_r);
                        }

            }

        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mShowClassTestRecordPopuWindown!=null && mShowClassTestRecordPopuWindown.isShowing()){
            mShowClassTestRecordPopuWindown.dismiss();

        }
    }
}
